<script setup lang="ts">
import { Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import useNotice from "@/view/Notice/utils/hook.tsx";
import Dialog from "@/view/Notice/dialog.vue";
import { onMounted } from "vue";
import { formattedDate } from "@/utils/formatDate.ts";
import { getNoticeList } from "@/api/notice.ts";

const {
  noticeList,
  dialog,
  params,
  handleAdd,
  handleGetList,
  handleEdit,
  handleDelete
} = useNotice()

onMounted(() => {
  handleGetList()
})
</script>

<template>
  <el-card class="box-card">
    <div class="card-equip-title">
      <div class="row align_center">
        <el-date-picker
          v-model="params.time"
          type="date"
          placeholder="选择日期"
        />
        <el-button style="margin-left: 20px" type="primary" @click="handleGetList">查找公告列表</el-button>
      </div>
      <el-button @click="handleAdd" type="primary">添加公告</el-button>
    </div>
    <div class="card-equip-table">
      <el-table
        border
        class="el-table-box"
        ref="multipleTableRef"
        :data="noticeList"
        style="width: 100%"
      >
        <el-table-column label="#" width="60" align="center" type="index"/>
        <el-table-column prop="title" align="center" label="标题"/>
        <el-table-column prop="publisher" align="center" label="发布人">
          <template #default="{row}">
            {{ row.publisher.name }}
          </template>
        </el-table-column>
        <el-table-column prop="content" align="center" label="内容">
          <template #default="{row}">
            <div class="notice-content">
              {{ row.content }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" align="center" label="发布时间">
          <template #default="{row}">
            {{ formattedDate(row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column prop="endDate" align="center" label="结束时间">
          <template #default="{row}">
            {{ formattedDate(row.endDate) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="控制" width="180">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" :icon="Edit as any" circle @click="handleEdit(row)"/>
              <el-popconfirm
                width="200"
                @confirm="handleDelete(row._id)"
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="InfoFilled as any"
                icon-color="#f36b6c"
                title="您确定删除此公告吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-card>

  <Dialog ref="dialog" @get="handleGetList"/>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-equip-title {
    height: 35px;
    display: flex;
    justify-content: space-between;
  }

  .card-equip-table {
    margin-top: 20px;

    .el-table-box {
      height: calc(90vh - 140px);

      .el-table__cell {
        position: static !important; // 解决el-image 和 el-table冲突层级冲突问题
      }
    }

    .equip-desc {
      display: -webkit-box; /*弹性伸缩盒子*/
      -webkit-box-orient: vertical; /*垂直排列*/
      line-height: 20px;
      -webkit-line-clamp: 1;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

.notice-content {
  display: -webkit-box; /*弹性伸缩盒子*/
  -webkit-box-orient: vertical; /*垂直排列*/
  line-height: 20px;
  -webkit-line-clamp: 2; /*设置行数*/
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
